import * as cc from "cc";
import yyComponent from "../../Script/Common/yyComponent";
import { EventType } from "../../Script/GameSpecial/GameEventType";
import { GlobalEnum } from "../../Script/GameSpecial/GlobalEnum";
import SkillBtn from "./SkillBtn";


const { ccclass, property } = cc._decorator;
//关卡中的控制器，接收玩家操作
@ccclass
export default class LevelController extends yyComponent {

    public init() {
        this.initJoy();
        this.initKeyMap();
        this.initKeyState();
        this.initSkillBtns();
        this.initCustomUpdateState();
        this.registAllCustomUpdate();
        this.onEvents();
    }
    protected onEvents() {
        this.on(EventType.Common.CtrlEvent.ctrlStart, this.setEnable, this);
        this.on(EventType.Common.CtrlEvent.ctrlEnd, this.setDisable, this);

        this.on(EventType.LevelEvent.updatePlayerSkillCool, this.onUpdatePlayerSkillCool, this);
        this.on(EventType.Common.CtrlEvent.btnTouchStart, this.onBtnTouchStart, this);
        this.on(EventType.Common.CtrlEvent.btnTouchEnd, this.onBtnTouchEnd, this);

        if (this.showFlowTouch) {
            //摇杆自动隐藏，全屏接受摇杆操作的方案：
            this.node.on(cc.Node.EventType.TOUCH_START, this.onJoyTouchStart, this);
            this.node.on(cc.Node.EventType.TOUCH_MOVE, this.onJoyTouchMove, this);
            this.node.on(cc.Node.EventType.TOUCH_END, this.onJoyTouchEnd, this);
            this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.onJoyTouchEnd, this);
        } else {
            //摇杆一直显示，只能在摇杆范围内操作的方案：
            this.joyPanel.on(cc.Node.EventType.TOUCH_START, this.onJoyTouchStart, this);
            this.joyPanel.on(cc.Node.EventType.TOUCH_MOVE, this.onJoyTouchMove, this);
            this.joyPanel.on(cc.Node.EventType.TOUCH_END, this.onJoyTouchEnd, this);
            this.joyPanel.on(cc.Node.EventType.TOUCH_CANCEL, this.onJoyTouchEnd, this);
        }

        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);
    }

    public reset() {
        this.resetJoy();
        this.resetKeyState();
        this.resetSimpleComps();
    }

    //启用触摸操作层
    public setEnable() {
        this.reset();
        this.node.active = true;
        this.setSkillBtnsImg();
    }
    //禁用触摸操作层
    public setDisable() {
        this.reset();
        this.node.active = false;
    }

    //#region 摇杆
    /**摇杆底盘图案，对应摇杆移动范围 */
    @property(cc.Node)
    protected joyPanel: cc.Node = null;
    protected joyPanelTrans: cc.UITransform = null;
    /**摇杆移动范围的半径 */
    protected joyScopeRadius: number = 50;
    /**摇杆移动范围的半径的平方 */
    protected joyScopeRadiusSqr: number = 2500;
    /**摇杆移动范围的中心点 */
    protected joyCenter: cc.Vec3 = null;
    /**摇杆当前位置相对中心点的方向 */
    protected joyDir: cc.Vec2 = null;
    /**触点按下时的坐标，决定了摇杆底盘的显示位置 */
    protected joyTouchStartPos: cc.Vec2 = null;
    /**摇杆是否跟随触点显示且不操作时隐藏 */
    protected showFlowTouch: boolean = true;
    /**摇杆中心圆点图案 */
    @property(cc.Node)
    protected joystick: cc.Node = null;
    protected initJoy() {
        this.joyDir = cc.v2();
        this.joyPanelTrans = this.joyPanel.getComponent(cc.UITransform);
        this.joyScopeRadius = this.joyPanelTrans.width * 0.5;
        this.joyScopeRadiusSqr = this.joyScopeRadius * this.joyScopeRadius;
        this.joyTouchStartPos = cc.v2();
        this.joyCenter = this.joyPanel.getPosition();
        this.joystick.setPosition(this.joyCenter);
        this.autoHideJoy();
    }
    protected resetJoy() {
        this.joyDir.x = 0;
        this.joyDir.y = 0;
        this.joystick.setPosition(this.joyCenter);
        this.autoHideJoy();
    }
    protected showJoy(p?: cc.Vec2) {
        if (!this.showFlowTouch) return;
        this.joyPanel.active = true;
        this.joystick.active = true;
        if (!!p) {
            this.joyCenter.x = p.x;
            this.joyCenter.y = p.y;
            this.joyPanel.setPosition(this.joyCenter);
            this.joystick.setPosition(this.joyCenter);
        }
    }
    /**触点松开时自动隐藏摇杆 */
    protected autoHideJoy() {
        if (this.showFlowTouch) {
            this.joyPanel.active = false;
            this.joystick.active = false;
        }
    }
    protected onJoyTouchStart(e: cc.EventTouch) {
        if (this.showFlowTouch) {
            e.getUILocation(this.joyTouchStartPos);
            this.showJoy(this.joyTouchStartPos);
            this.joyDir.x = 0;
            this.joyDir.y = 0;
        } else {
            this.setJoyToTouch(e);
        }
        this.convertJoyToBtn();
        this.emit(EventType.LevelEvent.startJoy, this.joyDir);
    }
    protected onJoyTouchMove(e: cc.EventTouch) {
        this.setJoyToTouch(e);
        this.convertJoyToBtn();
        this.emit(EventType.LevelEvent.moveJoy, this.joyDir);
    }
    protected onJoyTouchEnd(e: cc.EventTouch) {
        this.resetJoy();
        this.convertJoyToBtn();
        let p = e.getUILocation();
        this.emit(EventType.LevelEvent.stopJoy, p);
    }
    protected setJoyToTouch(e: cc.EventTouch) {
        let p = e.getUILocation();
        this.joyDir.x = p.x - this.joyCenter.x;
        this.joyDir.y = p.y - this.joyCenter.y;
        if (this.joyDir.lengthSqr() > this.joyScopeRadiusSqr) {
            this.joyDir.normalize().multiplyScalar(this.joyScopeRadius);
            p.x = this.joyCenter.x + this.joyDir.x;
            p.y = this.joyCenter.y + this.joyDir.y;
        }
        this.joystick.setPosition(p.x, p.y, 0);
    }
    //将摇杆位置转换到左右、跳跃键
    protected convertJoyToBtn() {
        return;
        if (this.joyDir.x > this.joyScopeRadius * 0.3) {

            if (this.keyStay[GlobalEnum.KeyCode.left]) {
                this.emit(EventType.LevelEvent.keyUp, GlobalEnum.KeyCode.left);
            }
            this.keyStay[GlobalEnum.KeyCode.left] = false;

            if (!this.keyStay[GlobalEnum.KeyCode.right]) {
                this.emit(EventType.LevelEvent.keyDown, GlobalEnum.KeyCode.right);
            }
            this.keyStay[GlobalEnum.KeyCode.right] = true;

        } else if (this.joyDir.x < this.joyScopeRadius * -0.3) {

            if (this.keyStay[GlobalEnum.KeyCode.right]) {
                this.emit(EventType.LevelEvent.keyUp, GlobalEnum.KeyCode.right);
            }
            this.keyStay[GlobalEnum.KeyCode.right] = false;

            if (!this.keyStay[GlobalEnum.KeyCode.left]) {
                this.emit(EventType.LevelEvent.keyDown, GlobalEnum.KeyCode.left);
            }
            this.keyStay[GlobalEnum.KeyCode.left] = true;

        } else {
            if (this.keyStay[GlobalEnum.KeyCode.left]) {
                this.emit(EventType.LevelEvent.keyUp, GlobalEnum.KeyCode.left);
                this.keyStay[GlobalEnum.KeyCode.left] = false;
            }

            if (this.keyStay[GlobalEnum.KeyCode.right]) {
                this.emit(EventType.LevelEvent.keyUp, GlobalEnum.KeyCode.right);
                this.keyStay[GlobalEnum.KeyCode.right] = false;
            }
        }

        if (this.joyDir.y > this.joyScopeRadius * 0.4) {
            if (!this.keyStay[GlobalEnum.KeyCode.up]) {
                this.emit(EventType.LevelEvent.keyDown, GlobalEnum.KeyCode.up);
            }
            this.keyStay[GlobalEnum.KeyCode.up] = true;
        } else {
            if (this.keyStay[GlobalEnum.KeyCode.up]) {
                this.emit(EventType.LevelEvent.keyUp, GlobalEnum.KeyCode.up);
            }
            this.keyStay[GlobalEnum.KeyCode.up] = false;
        }
    }
    //#endregion

    //#region 技能按钮
    @property([SkillBtn])
    protected skillBtns: SkillBtn[] = [];
    protected initSkillBtns() {
        for (let i = 0; i < this.skillBtns.length; ++i) {
            this.addSimpleComp(this.skillBtns[i]);
        }
        // this.skillBtns[0].setBtnId(GlobalEnum.KeyCode.atk1);
        // this.skillBtns[1].setBtnId(GlobalEnum.KeyCode.atk2);
        // this.skillBtns[2].setBtnId(GlobalEnum.KeyCode.atk3);
        // this.skillBtns[3].setBtnId(GlobalEnum.KeyCode.shanBi);
        this.setSkillBtnsImg();
    }
    protected setSkillBtnsImg() {
        // let id = PlayerData.getCurSkinId(GlobalEnum.GameDataType.playerSkin);
        // let roleName = GameData.getData(GlobalEnum.GameDataType.roleData, id).roleName;
        // this.skillBtns[1].setSkill(roleName + "Atk2");
        // this.skillBtns[2].setSkill(roleName + "Atk3");
    }
    //#endregion

    /**获取全部按钮的世界坐标 */
    public getBtnPos() {

        let data = {
        };
        return data;
    }

    public getBtns() {
        return {

        }
    }

    /******************************************************按键事件 ******************************************************/
    /**按键是否持续按下状态 */
    protected keyStay: boolean[];
    protected initKeyState() {
        this.keyStay = [];
        for (let i = 0; i < GlobalEnum.KeyCode.count; ++i) {
            this.keyStay.push(false);
        }
    }
    protected resetKeyState() {
        for (let i = this.keyStay.length - 1; i >= 0; --i) {
            this.keyStay[i] = false;
        }
    }

    /******************************************************PC端按键事件 ******************************************************/
    //#region 键盘按键
    /**键盘按键与自定义按键对应关系 */
    protected keyMap: any = {};
    protected initKeyMap() {
        let key = cc.macro.KEY;
        this.keyMap[key.w] = GlobalEnum.KeyCode.up;
        this.keyMap[key.a] = GlobalEnum.KeyCode.left;
        this.keyMap[key.s] = GlobalEnum.KeyCode.down;
        this.keyMap[key.d] = GlobalEnum.KeyCode.right;
        // this.keyMap[key.h] = GlobalEnum.KeyCode.shanBi;
        // this.keyMap[key.j] = GlobalEnum.KeyCode.atk1;
        // this.keyMap[key.k] = GlobalEnum.KeyCode.atk2;
        // this.keyMap[key.l] = GlobalEnum.KeyCode.atk3;
    }

    protected onKeyDown(e: cc.EventKeyboard) {
        let id = e.keyCode;
        let stay = this.keyStay[this.keyMap[id]];
        this.keyStay[this.keyMap[id]] = true;
        if (false === stay) {
            this.emit(EventType.LevelEvent.keyDown, this.keyMap[id]);
        }
    }

    protected onKeyUp(e: cc.EventKeyboard) {
        let id = e.keyCode;
        let stay = this.keyStay[this.keyMap[id]];
        this.keyStay[this.keyMap[id]] = false;
        if (!!stay) {
            this.emit(EventType.LevelEvent.keyUp, this.keyMap[id]);
        }
    }
    //#endregion

    protected stepKeyStay(dt: number) {
        for (let i = this.keyStay.length - 1; i >= 0; --i) {
            if (this.keyStay[i]) {
                this.emit(EventType.LevelEvent.keyStay, i);
            }
        }
    }

    update(dt) {
        this.stepKeyStay(dt);
    }

    /*******************************************************事件回调*******************************************************/
    /**更新全部技能冷却状态 */
    protected onUpdatePlayerSkillCool(data: { [skillId: number]: any }) {
        this.skillBtns[1].setCoolPrecess(data[2]);
        this.skillBtns[2].setCoolPrecess(data[3]);
        this.skillBtns[3].setCoolPrecess(data[4]);
    }

    protected onBtnTouchStart(id) {
        let stay = this.keyStay[id];
        this.keyStay[id] = true;
        if (false === stay) {
            this.emit(EventType.LevelEvent.keyDown, id);
        }
    }
    protected onBtnTouchEnd(id) {
        let stay = this.keyStay[id];
        this.keyStay[id] = false;
        if (!!stay) {
            this.emit(EventType.LevelEvent.keyUp, id);
        }
    }
}
